package com.ai.mapper.made;

import com.ai.entity.made.camera.CameraPO;
import com.ai.entity.made.camera.CameraVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author Mr.Chen
 * @since 2020-06-17
 */
@Repository
public interface CameraMapper extends BaseMapper<CameraPO> {

    @Select("SELECT\n" +
            "camera.id id,\n" +
            "camera.code code,\n" +
            "camera.nickname nickname,\n" +
            "camera.username username,\n" +
            "camera.password password,\n"+
            "camera.ip ip,\n" +
            "camera.url url,\n" +
            "camera.state state,\n" +
            "camera.port port,\n" +
            "camera.stream_way streamWay,\n" +
            "server.nickname serverNickname,\n" +
            "server.ip bindServerAdd,\n"+
            "camera.create_time createTime,\n" +
            "camera.remark remark ,\n" +
            "reb.region_name ,\n"+
            "reb.region_code \n"+
            "FROM\n" +
            "camera\n" +
            "LEFT JOIN `server` ON camera.server_id = `server`.id\n" +
            "LEFT JOIN  `region_camera_manage` rem ON  rem.camera_id = camera.id\n"+
            "LEFT JOIN  `region_base` reb ON  reb.id=rem.region_id\n"+
            " ${ew.customSqlSegment}")
    IPage<CameraVO> cameraList(IPage<CameraPO> page, @Param("ew") QueryWrapper<CameraPO> queryWrapper);

    @Select("SELECT\n" +
            "camera.*\n" +
            "FROM\n" +
            "camera\n" +
            "LEFT JOIN camera_function ON camera_function.camera_id = camera.id\n" +
            "LEFT JOIN functions ON camera_function.function_id = functions.id \n" +
            "${ew.customSqlSegment}")
    IPage<CameraPO> listByPersonFlow(IPage<CameraPO> page, @Param("ew") QueryWrapper<CameraPO> queryWrapper);

    @Select("select  id  from  camera where state = 1")
    List<Integer> openCameraIdList();

    @Select("select  id  from  camera GROUP BY id limit #{start},#{size} ")
    List<Integer> pageCameraIdList(int start, int size);

    @Select("select  id  from  camera where code = #{code}")
    Integer getByCode (String code);

    @Select("select c.id,c.nickname " +
            "from camera c " +
            "LEFT JOIN camera_functions cf on c.id = cf.camera_id " +
            "LEFT JOIN functions f on f.id = cf.function_id " +
            "where f.type = #{type}")
    List<CameraPO>  selectByFunctionType(int type);


    @Select("select * from camera where server_id = #{serverId}")
    List<CameraPO>  checkServer(int serverId);

}
